/*
 * Copyright (c) 2010. raoqn
 */

package com.yfeng.goodjuhome.service.impl;

import com.yfeng.common.dao.IDao;
import com.yfeng.common.dao.Param;
import com.yfeng.common.utils.DateUtil;
import com.yfeng.common.utils.Util;
import com.yfeng.exception.ServiceException;
import com.yfeng.goodjuhome.model.VisitModel;
import com.yfeng.goodjuhome.service.IVisitService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * www.goodjuhome.com
 * Created by IntelliJ IDEA.
 * User: rqn
 * Date: 2010-8-14
 * Time: 23:36:04
 */
@Service("visitService")
public class VisitServiceImpl implements IVisitService {

     private final Log log = LogFactory.getLog(this.getClass());
    @Autowired
    public IDao baseDao;

    /**
     * 添加访问者
     * @param req
     * @param uid 访问者ID
     * @param blog_uid 被访问者的ID
     * @return
     * @throws ServiceException
     */
    public int addVisit(HttpServletRequest req, String uid, String blog_uid) throws ServiceException {
         try
       {
           
           String ip = req.getRemoteAddr();
           String browse = req.getHeader("user-agent");
           Param p = new Param();
           p.put("visit_uid",uid);
           p.put("blog_uid",blog_uid);
           p.put("ip",ip);
           p.put("browse",browse);
           p.put("url",req.getRequestURL());
           p.put("createtime", DateUtil.getCurDateTime());
           int ret = baseDao.save("visits",p);
           return ret;
       }
       catch(Exception e)
       {
           log.error(e.getMessage(),e);
           throw new ServiceException(e.getMessage(),e);
       }
    }

    public int deleteVisit(String[] id) throws ServiceException {
         try {

            for (int i = 0; i < id.length; i++) {
                baseDao.delete("visits", new Param("id", id[i]));
            }
            return 1;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new ServiceException(e.getMessage(), e);

        }
    }

    /**
     * 查询用户访问了哪些blog
     *
     * @param uid
     * @return
     * @throws com.yfeng.exception.ServiceException
     *
     */
    public List queryBlogByUid(String uid) throws ServiceException {
         try {
            List result = baseDao.query("select * from visits where visit_uid=:uid", new Param("uid", uid));
            return result;
        }
        catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new ServiceException(e.getMessage(), e);
        }
    }

    /**
     * 查询该用户的BLOG被哪些用户访问过
     *
     * @param uid
     * @return
     * @throws com.yfeng.exception.ServiceException
     *
     */
    public List queryVisitByUid(String uid) throws ServiceException {
      try {
            List result = baseDao.query("select * from visits where blog_uid=:uid", new Param("uid", uid));
            return result;
        }
        catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new ServiceException(e.getMessage(), e);
        }
    }

    /**
     * 查看某个访问记录的详细信息
     *
     * @param id
     * @return
     * @throws com.yfeng.exception.ServiceException
     *
     */
    public VisitModel getVisit(String id) throws ServiceException {
        try {
            List result = baseDao.query("select * from visits where id=:iid", new Param("id", id));
            if(result.size()>0)
            {
                Map map = (Map)result.get(0);
                VisitModel model = new VisitModel();
                model.setId(id);
                model.setBlog_uid(Util.getEmptyStr(map.get("blog_uid")));
                model.setVisit_uid(Util.getEmptyStr(map.get("visit_uid")));
                model.setIp(Util.getEmptyStr(map.get("ip")));
                model.setBrowse(Util.getEmptyStr(map.get("browse")));
                model.setUrl(Util.getEmptyStr(map.get("url")));
                return model;
            }
            else
                return null;
        }
        catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new ServiceException(e.getMessage(), e);
        }
    }
}
